var ws;
var wsURL;
var schema = location.protocol;
var host = location.host;
if (schema === 'https:') {
    wsURL = "wss://" + host + "/wshandler";
} else {
    wsURL = "ws://" + host + "/wshandler";
}
$(function () {
    function connect(stype) {
        ws = new WebSocket(wsURL);
        ws.onopen = function () {
            console.log("->Connected..");
            var ping = pinger(ws);
            if (ws.readyState == WebSocket.OPEN) {
                ws.send('{"event": "' + stype + '"}');
            }
            else {
                console.log("->Connection is closed");
            }
        };
        ws.onmessage = function (evt) {
            if (evt.data !== '') {
                var obj = jQuery.parseJSON(evt.data);
                if (obj.dashb !== undefined) {
                    if (lastprice !== obj.dashb.price) {
                    }
                        for (var i in obj.txns) {
                            startstopwatch(obj.txns[i].t_time, "cd" + obj.txns[i].t_hash)
                        }
                    }
                }
        };
        ws.onerror = function (evt) {
            console.log("->socket error");
        };
        ws.onclose = function () {
            var d = new Date();
            console.log("->disconnected..");
        };
    }
    connect("h");
   /* $('#togglesocket').change(function () {
        if ($(this).is(":checked")) {
            connect("gsnew");
        } else {
            ws.close();
        }
    });*/
});

function pinger(ws) {
    var timer = setInterval(function () {
        if (ws.readyState == 1) {
            ws.send(JSON.stringify({event: "ping"}));
            console.log(JSON.stringify({event: "ping"}));
        }
    }, 60000);
    return {
        stop: function () {
            clearInterval(timer);
        }
    };
}